<template>
  <div class="layout-route">
      <div class="card card-search">
          <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="采购批次号" prop="contract_number">
                    <el-input v-model.trim="queryParams.contract_number" placeholder="请输入采购批次号" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="SPU" prop="spu">
                    <el-input v-model.trim="queryParams.spu" placeholder="请输入SPU" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="供应商" prop="supply_id">
                    <SelectPage v-model="queryParams.supply_id" placeholder="请选择供应商" :url="$api.supply.getSupplyList" show_field="supply_name" query_field="supply_name" query_field2="spu" filterable clearable popper-class="custom-select" style="width: 240px" ></SelectPage>
                </el-form-item>
                <el-form-item label="采购日期" prop="purchase_time">
                    <el-date-picker v-model="queryParams.purchase_time" format="yyyy-MM-dd" type="date" placeholder="采购日期" style="width: 240px"></el-date-picker>
                </el-form-item>
                <el-form-item label="到货状态" prop="arrival_status">
                    <el-select v-model="queryParams.arrival_status" placeholder="请选择到货状态" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in arrivalStatus" :key="index" :label="dict.arrival_status" :value="dict.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="交货进度" prop="delivery_status">
                    <el-select v-model="queryParams.delivery_status" placeholder="请选择交货进度" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in deliveryStatus" :key="index" :label="dict.delivery_status" :value="dict.id" />
                    </el-select>
                </el-form-item>
               <el-form-item label=" ">
                  <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                  <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
              </el-form-item>
          </el-form>
      </div>

      <div class="card">
          <el-row :gutter="10" class="mb8">
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>
          <el-table v-loading="loading" :data="tableData" border script>
              <el-table-column label="采购批次号" prop="contract_number" min-width="160" show-overflow-tooltip></el-table-column>
              <el-table-column label="采购单号" prop="purchase_code" min-width="160" show-overflow-tooltip></el-table-column>
              <el-table-column label="SPU" prop="spu" min-width="120" show-overflow-tooltip></el-table-column>
              <el-table-column label="采购日期" prop="purchase_time" min-width="160" show-overflow-tooltip></el-table-column>
              <el-table-column label="商品名称" prop="product_name" min-width="200" show-overflow-tooltip></el-table-column>
              <el-table-column label="供应商" prop="supply_name" min-width="200" show-overflow-tooltip></el-table-column>
              <el-table-column label="预计交货数量" prop="dev_delivery_nums" min-width="120" show-overflow-tooltip></el-table-column>
              <el-table-column label="实际交货数量" prop="real_delivery_nums" min-width="120" show-overflow-tooltip></el-table-column>
              <el-table-column label="到货状态" prop="arrival_status" min-width="100">
                  <template slot-scope="scope">
                      {{ scope.row.arrival_status | formatLabelByValueFuzzy(arrivalStatus, { label: 'arrival_status', value: 'id'}) }}
                  </template>
              </el-table-column>
              <el-table-column label="交货进度" prop="delivery_status" min-width="100">
                  <template slot-scope="scope">
                      {{ scope.row.delivery_status | formatLabelByValueFuzzy(deliveryStatus, { label: 'delivery_status', value: 'id'}) }}
                  </template>
              </el-table-column>
              <el-table-column label="备注" prop="remark" min-width="160" show-overflow-tooltip></el-table-column>
              <el-table-column label="附件" prop="attachment_images" min-width="100">
                    <template slot-scope="scope">
                        <ImageUploadImg v-model="scope.row.attachment_images" :readOnly="true"></ImageUploadImg>
                    </template>
                </el-table-column>
              <el-table-column label="创建人" prop="create_operator" min-width="100" show-overflow-tooltip></el-table-column>
              <el-table-column label="修改人" prop="update_operator" min-width="100" show-overflow-tooltip></el-table-column>
              <el-table-column label="创建时间" prop="create_time" min-width="160"></el-table-column>
              <el-table-column label="更新时间" prop="update_time" min-width="160"></el-table-column>
          </el-table>
          <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
      </div>
  </div>
</template>

<script>
import { getPurchaseList, getArrivalStatus, getDeliveryStatus } from '@/api/purchase'
export default {
  name: 'PurchaseList',
  data() {
      return {
          // 遮罩层
          loading: true,
          // 显示搜索条件
          showSearch: true,
          // 总条数
          total: 0,
          // 表格数据
          tableData: [],
          arrivalStatus: [],
          deliveryStatus: [],
          pageInfo: {
              page: 1,
              limit: 10,
          },
          // 查询参数
          queryParams: {
            spu: undefined,
            contract_number: undefined,
            supply_id: undefined,
            purchase_time: undefined,
            arrival_status: undefined,
            delivery_status: undefined,
          },
      }
  },
  async created() {
      this.loading = true
      try {
            const [arrivalData, deliveryData] = await Promise.all([getArrivalStatus(), getDeliveryStatus()])
            this.arrivalStatus = arrivalData?.data ?? []
            this.deliveryStatus = deliveryData?.data ?? []
        } catch (error) {
            this.loading = false
            console.log(error)
        }
      this.getList()
  },
  methods: {
      //列表
      getList() {
          this.loading = true
          const qyparams = {
              ...this.pageInfo,
              ...this.queryParams,
          }
          getPurchaseList(qyparams)
              .then(response => {
                    // this.tableData = response.data
                  this.tableData = response.data.list
                  this.total = response.data.total
              })
              .finally(() => {
                  this.loading = false
              })
      },

      // 搜索按钮操作
      handleQuery() {
          this.pageInfo.page = 1
          this.getList()
      },

      // 重置按钮操作
      resetQuery() {
          this.resetForm('queryForm')

          this.handleQuery()
      },
  },
}
</script>
